home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / UXSOCK.C < prev    next >
C/C++ Source or Header  |  1992-02-26  |  7KB  |  255 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/src/microcode/RCS/uxsock.c,v 1.9 1992/02/27 02:14:00 jinx Exp $
  4.  
  5. Copyright (c) 1990-1992 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. #include "ux.h"
  36.  
  37. #ifdef HAVE_SOCKETS
  38.  
  39. #include <sys/socket.h>
  40. #include <netinet/in.h>
  41. #include <netdb.h>
  42. #ifdef HAVE_UNIX_SOCKETS
  43. #include <sys/un.h>
  44. #endif
  45. #include "uxsock.h"
  46. #include "uxio.h"
  47. #include "prims.h"
  48.  
  49. extern struct servent * EXFUN (getservbyname, (CONST char *, CONST char *));
  50. extern struct hostent * EXFUN (gethostbyname, (CONST char *));
  51.  
  52. Tchannel
  53. DEFUN (OS_open_tcp_stream_socket, (host, port), char * host AND int port)
  54. {
  55.   int s;
  56.   STD_UINT_SYSTEM_CALL
  57.     (syscall_socket, s, (UX_socket (AF_INET, SOCK_STREAM, 0)));
  58.   {
  59.     struct sockaddr_in address;
  60.     (address . sin_family) = AF_INET;
  61.     {
  62.       char * scan = ((char*) (& (address . sin_addr)));
  63.       char * end = (scan + (sizeof (address . sin_addr)));
  64.       while (scan < end)
  65.     (*scan++) = (*host++);
  66.     }
  67.     (address . sin_port) = port;
  68.     STD_VOID_SYSTEM_CALL
  69.       (syscall_connect, (UX_connect (s, (&address), (sizeof (address)))));
  70.   }
  71.   MAKE_CHANNEL (s, channel_type_tcp_stream_socket, return);
  72. }
  73.  
  74. int
  75. DEFUN (OS_get_service_by_name, (service_name, protocol_name),
  76.        CONST char * service_name AND
  77.        CONST char * protocol_name)
  78. {
  79.   struct servent * entry = (UX_getservbyname (service_name, protocol_name));
  80.   return ((entry == 0) ? (-1) : (entry -> s_port));
  81. }
  82.  
  83. unsigned int
  84. DEFUN_VOID (OS_host_address_length)
  85. {
  86.   return (sizeof (struct in_addr));
  87. }
  88.  
  89. char **
  90. DEFUN (OS_get_host_by_name, (host_name), CONST char * host_name)
  91. {
  92.   struct hostent * entry = (UX_gethostbyname (host_name));
  93.   if (entry == 0)
  94.     return (0);
  95. #ifndef USE_HOSTENT_ADDR
  96.   return (entry -> h_addr_list);
  97. #else
  98.   {
  99.     static char * addresses [2];
  100.     (addresses[0]) = (entry -> h_addr);
  101.     (addresses[1]) = 0;
  102.     return (addresses);
  103.   }
  104. #endif
  105. }
  106.  
  107. Tchannel
  108. DEFUN (OS_open_unix_stream_socket, (filename), CONST char * filename)
  109. {
  110. #ifdef HAVE_UNIX_SOCKETS
  111.   int s;
  112.   extern char * EXFUN (strncpy, (char *, CONST char *, size_t));
  113.   STD_UINT_SYSTEM_CALL
  114.     (syscall_socket, s, (UX_socket (AF_UNIX, SOCK_STREAM, 0)));
  115.   {
  116.     struct sockaddr_un address;
  117.     (address . sun_family) = AF_UNIX;
  118.     strncpy ((address . sun_path), filename, (sizeof (address . sun_path)));
  119.     STD_VOID_SYSTEM_CALL
  120.       (syscall_connect, (UX_connect (s, (&address), (sizeof (address)))));
  121.   }
  122.   MAKE_CHANNEL (s, channel_type_unix_stream_socket, return);
  123. #else /* not HAVE_UNIX_SOCKETS */
  124.   error_unimplemented_primitive ();
  125.   return (NO_CHANNEL);
  126. #endif /* not HAVE_UNIX_SOCKETS */
  127. }
  128.  
  129. #ifndef SOCKET_LISTEN_BACKLOG
  130. #define SOCKET_LISTEN_BACKLOG 5
  131. #endif
  132.  
  133. Tchannel
  134. DEFUN (OS_open_server_socket, (port, ArgNo), unsigned int port AND int ArgNo)
  135. {
  136.   int s;
  137.  
  138.   if (((sizeof (unsigned int)) >
  139.        (sizeof (((struct sockaddr_in *) 0)->sin_port))) &&
  140.       (port >= (1 << (CHAR_BIT
  141.               * (sizeof (((struct sockaddr_in *) 0)->sin_port))))))
  142.     error_bad_range_arg(ArgNo);    
  143.   STD_UINT_SYSTEM_CALL
  144.     (syscall_socket, s, (UX_socket (AF_INET, SOCK_STREAM, 0)));
  145.   {
  146.     struct sockaddr_in address;
  147.     (address . sin_family) = AF_INET;
  148.     (address . sin_addr . s_addr) = INADDR_ANY;
  149.     (address . sin_port) = port;
  150.     STD_VOID_SYSTEM_CALL
  151.       (syscall_bind, (UX_bind (s, (&address), (sizeof (struct sockaddr_in)))));
  152.   }
  153.   STD_VOID_SYSTEM_CALL
  154.     (syscall_listen, (UX_listen (s, SOCKET_LISTEN_BACKLOG)));
  155.   MAKE_CHANNEL (s, channel_type_tcp_server_socket, return);
  156. }
  157.  
  158. Tchannel
  159. DEFUN (OS_server_connection_accept, (channel, peer_host, peer_port),
  160.        Tchannel channel AND
  161.        char * peer_host AND
  162.        int * peer_port)
  163. {
  164.   static struct sockaddr_in address;
  165.   int address_length = (sizeof (struct sockaddr_in));
  166.   int s;
  167.  
  168.   while ((s = (UX_accept ((CHANNEL_DESCRIPTOR (channel)),
  169.               (&address),
  170.               (&address_length))))
  171.      < 0)
  172.   {
  173.     if (errno != EINTR)
  174.     {
  175. #ifdef EAGAIN
  176.       if (errno == EAGAIN)
  177.     return (NO_CHANNEL);
  178. #endif
  179. #ifdef EWOULDBLOCK
  180.       if (errno == EWOULDBLOCK)
  181.     return (NO_CHANNEL);
  182. #endif
  183.       error_system_call (errno, syscall_accept);
  184.     }
  185.   }
  186.   if (peer_host != 0)
  187.     {
  188.       char * scan = ((char *) (& (address . sin_addr)));
  189.       char * end = (scan + (sizeof (address . sin_addr)));
  190.       while (scan < end)
  191.     (*peer_host++) = (*scan++);
  192.     }
  193.   if (peer_port != 0)
  194.     (*peer_port) = (address . sin_port);
  195.   MAKE_CHANNEL (s, channel_type_tcp_stream_socket, return);
  196. }
  197.  
  198. #else /* not HAVE_SOCKETS */
  199.  
  200. Tchannel
  201. DEFUN (OS_open_tcp_stream_socket, (host, port), char * host AND int port)
  202. {
  203.   error_unimplemented_primitive ();
  204.   return (NO_CHANNEL);
  205. }
  206.  
  207. int
  208. DEFUN (OS_get_service_by_name, (service_name, protocol_name),
  209.        CONST char * service_name AND
  210.        CONST char * protocol_name)
  211. {
  212.   error_unimplemented_primitive ();
  213.   return (-1);
  214. }
  215.  
  216. unsigned int
  217. DEFUN_VOID (OS_host_address_length)
  218. {
  219.   error_unimplemented_primitive ();
  220.   return (0);
  221. }
  222.  
  223. char **
  224. DEFUN (OS_get_host_by_name, (host_name), CONST char * host_name)
  225. {
  226.   error_unimplemented_primitive ();
  227.   return (0);
  228. }
  229.  
  230. Tchannel
  231. DEFUN (OS_open_unix_stream_socket, (filename), CONST char * filename)
  232. {
  233.   error_unimplemented_primitive ();
  234.   return (NO_CHANNEL);
  235. }
  236.  
  237. Tchannel
  238. DEFUN (OS_open_server_socket, (port, ArgNo), unsigned int port AND int ArgNo)
  239. {
  240.   error_unimplemented_primitive ();
  241.   return (NO_CHANNEL);
  242. }
  243.  
  244. Tchannel
  245. DEFUN (OS_server_connection_accept, (channel, peer_host, peer_port),
  246.        Tchannel channel AND
  247.        char * peer_host AND
  248.        int * peer_port)
  249. {
  250.   error_unimplemented_primitive ();
  251.   return (NO_CHANNEL);
  252. }
  253.  
  254. #endif /* not HAVE_SOCKETS */
  255.